'Copyright 2006 James Downey

Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk
Imports FetchXMLBuilder.CrmServiceSdk

Public Class frmMain

    Dim Fetch As Fetch
    Public CrmServiceURL As String
    Private SuppressEntityChangeAlert As Boolean = False

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim frm As New frmFetchMetadata(Me)
        Do While 1 = 1
            Select Case frm.ShowDialog
                Case Windows.Forms.DialogResult.OK
                    LoadEntities()
                    Exit Do
                Case Windows.Forms.DialogResult.Retry
                    'retry
                Case Else
                    Me.Close()
                    Exit Do
            End Select
        Loop
    End Sub

    Private Sub FetchMetadataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FetchMetadataToolStripMenuItem.Click
        If Windows.Forms.MessageBox.Show("Retrieving new metadata will remove all query information from the form.", Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Dim frm As New frmFetchMetadata(Me)
            If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
                LoadEntities()
                Me.lstAttributes.Items.Clear()
                Me.lstFilters.Items.Clear()
                Me.lstLinks.Items.Clear()
                Me.lstOrder.Items.Clear()
                Me.chkAllAttributes.Checked = False
                Me.Fetch = Nothing
                Me.txtResult.Text = ""
                Me.lblResults.Text = ""
                Me.txtQuery.Text = ""
            End If
        End If
    End Sub

    Public Sub SetMetadata(ByVal Metadata As Metadata)
        CurrentMetadata = Metadata
    End Sub

    Private Sub lstEntity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstEntity.SelectedIndexChanged
        If Not SuppressEntityChangeAlert And Not Fetch Is Nothing AndAlso Me.lstEntity.SelectedItem.ToString <> Me.Fetch.Entity.Name Then
            If Windows.Forms.MessageBox.Show("Changing the entity will remove all filters and linked-entities in the query.", Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Cancel Then
                Me.lstEntity.SelectedItem = Me.lstEntity.Items(Me.lstEntity.FindString(Me.Fetch.Entity.Name))
                Exit Sub
            End If
        End If
        If Fetch Is Nothing OrElse Me.lstEntity.SelectedItem.ToString <> Me.Fetch.Entity.Name Then
            Fetch = New Fetch(Me.lstEntity.SelectedItem.ToString)
            LoadAttributes()
            LoadFilters()
            LoadLinkedEntities()
            LoadOrders()
        End If
    End Sub

    Public Sub LoadFilters()
        Me.lstFilters.Items.Clear()
        Dim filter As Filter
        For Each filter In Fetch.Entity.Filters
            Me.lstFilters.Items.Add(filter)
        Next
        Me.lblResults.Text = ""
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
    End Sub

    Public Sub LoadAttributes()
        Me.lstAttributes.Items.Clear()
        Dim attrib As Attribute
        For Each attrib In Me.Fetch.Entity.Attributes
            Me.lstAttributes.Items.Add(attrib)
        Next
    End Sub

    Public Sub LoadLinkedEntities()
        Me.lstLinks.Items.Clear()
        Dim link As LinkedEntity
        For Each link In Fetch.Entity.LinkedEntities
            Me.lstLinks.Items.Add(link)
        Next
        Me.lblResults.Text = ""
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
    End Sub

    Public Sub LoadEntities()
        Me.lstEntity.Items.Clear()
        Dim i As Integer
        For i = 0 To CurrentMetadata.Entities.Length - 1
            Me.lstEntity.Items.Add(CurrentMetadata.Entities(i).Name)
        Next
    End Sub

    Public Sub LoadOrders()
        Me.lstOrder.Items.Clear()
        Dim order As Order
        For Each order In Me.Fetch.Entity.Orders
            Me.lstOrder.Items.Add(order)
        Next
        Me.lblResults.Text = ""
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
    End Sub

    Private Sub btnAddFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddFilter.Click
        If Me.lstEntity.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select an entity.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim filter As New Filter()
        Dim frmFilter As New frmFilter(filter, GetEntity(Me.Fetch.Entity.Name))
        If frmFilter.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Fetch.Entity.Filters.Add(filter)
        End If
        LoadFilters()
    End Sub

    Private Sub btnEditFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditFilter.Click
        If Me.lstFilters.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a filter.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim filter As Filter = Me.lstFilters.SelectedItem
        Dim frmFilter As New frmFilter(filter, GetEntity(Me.Fetch.Entity.Name))
        If frmFilter.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadFilters()
        End If
    End Sub

    Private Sub btnDeleteFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteFilter.Click
        If Me.lstFilters.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a filter.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        If Windows.Forms.MessageBox.Show("Are you sure that you want to delete this filter?", Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Fetch.Entity.Filters.Remove(Me.lstFilters.SelectedItem)
            LoadFilters()
        End If
    End Sub

    Private Sub btnEditOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditOrder.Click
        If Me.Fetch Is Nothing Then
            Windows.Forms.MessageBox.Show("Select an entity.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim frm As New frmOrder(Fetch.Entity.Orders, GetEntity(Me.Fetch.Entity.Name))
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.lstOrder.Items.Clear()
            Dim order As Order
            For Each order In Fetch.Entity.Orders
                Me.lstOrder.Items.Add(order)
            Next
        End If
    End Sub

    Private Sub btnAddLinkElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLinkElement.Click
        If Me.Fetch Is Nothing Then
            Windows.Forms.MessageBox.Show("Select an entity.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim frm As New frmLinkedEntity(Nothing, GetEntity(Me.Fetch.Entity.Name))
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Fetch.Entity.LinkedEntities.Add(frm.LinkedEntity)
            LoadLinkedEntities()
        End If
    End Sub

    Private Sub btnEditLinkElement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditLinkElement.Click
        If Me.lstLinks.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select a linked-entity.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Dim frm As New frmLinkedEntity(Me.lstLinks.SelectedItem, GetEntity(Me.Fetch.Entity.Name))
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.Fetch.Entity.LinkedEntities.Replace(Me.lstLinks.SelectedItem, frm.LinkedEntity)
            LoadLinkedEntities()
        End If
    End Sub

    Private Sub btnDeleteLink_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteLink.Click
        If Me.lstLinks.SelectedIndex = -1 Then Exit Sub
        If Windows.Forms.MessageBox.Show("Are you sure that you want to delete this linked-entity?", Application.ProductName, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            Me.Fetch.Entity.LinkedEntities.Remove(Me.lstLinks.SelectedItem)
            LoadLinkedEntities()
        End If
    End Sub

    Public Function FormatXML(ByVal ptrNode As Xml.XmlNode, Optional ByVal iTabLevel As Integer = 0) As String
        Dim bMixedTextNode As Boolean
        Dim bHasOnlyATextNode As Boolean
        Dim i As Integer
        Dim FormattedXML As String
        FormattedXML = ""
        With ptrNode
            Select Case .NodeType
                Case Xml.XmlNodeType.Document, Xml.XmlNodeType.DocumentFragment
                    For i = 0 To .ChildNodes.Count - 1
                        FormattedXML = FormattedXML & FormatXML(.ChildNodes(i))
                    Next
                Case Xml.XmlNodeType.Text
                    bMixedTextNode = (.ParentNode.ChildNodes.Count > 1)
                    If bMixedTextNode Then FormatXML = RepeatString(iTabLevel, vbTab)
                    FormattedXML = FormattedXML & Trim(Replace(Replace(.InnerXml, vbCrLf, " "), vbTab, " "))
                    If bMixedTextNode Then FormattedXML = FormattedXML & vbCrLf
                Case Xml.XmlNodeType.Element
                    If .HasChildNodes Then
                        bHasOnlyATextNode = (.ChildNodes(0).NodeType = Xml.XmlNodeType.Text) And (.ChildNodes.Count = 1)
                    End If
                    FormattedXML = RepeatString(iTabLevel, vbTab) & "<" & .Name
                    For i = 0 To .Attributes.Count - 1
                        FormattedXML = FormattedXML + FormatXML(.Attributes(i))
                    Next
                    If Not .HasChildNodes Then
                        FormattedXML = FormattedXML & "/>" & vbCrLf
                    Else
                        If bHasOnlyATextNode Then
                            FormattedXML = FormattedXML & ">" & .InnerText
                        Else
                            FormattedXML = FormattedXML & ">" & vbCrLf
                        End If
                        For i = 0 To .ChildNodes.Count - 1
                            FormattedXML = FormattedXML & FormatXML(.ChildNodes(i), iTabLevel + 1)
                        Next
                        If Not bHasOnlyATextNode Then FormattedXML = FormattedXML & RepeatString(iTabLevel, vbTab)
                        FormattedXML = FormattedXML & "</" & .Name & ">" & vbCrLf
                    End If

                Case Xml.XmlNodeType.Comment, Xml.XmlNodeType.CDATA
                    If InStr(1, .InnerXml, vbCr) = 0 Then FormatXML = RepeatString(iTabLevel, vbTab)
                    FormattedXML = FormattedXML & .InnerXml & vbCrLf
                Case Xml.XmlNodeType.Attribute
                    If InStr(1, .InnerText, Chr(34)) > 0 Then
                        FormattedXML = " " & .Name & "='" & .InnerText & "'"
                    Else
                        FormattedXML = " " & .Name & "=" & Chr(39) & .InnerText & Chr(39)
                    End If
                Case Xml.XmlNodeType.Entity
                Case Else
                    FormattedXML = RepeatString(iTabLevel, vbTab) & .InnerXml & vbCrLf
            End Select
        End With
        Return FormattedXML
    End Function

    Private Function RepeatString(ByVal NumberOfRepeats As Integer, ByVal RepeatingString As String)
        Dim i As Integer
        Dim Value As String
        Value = ""
        For i = 1 To NumberOfRepeats
            Value &= RepeatingString
        Next
        Return Value
    End Function

    Private Sub btnCreateXML_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateXML.Click
        If Me.lstEntity.SelectedIndex = -1 Then
            Windows.Forms.MessageBox.Show("Select an entity.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
        Try
            Me.Cursor = Cursors.WaitCursor
            Me.Fetch.Entity.AllAttributes = Me.chkAllAttributes.Checked
            Me.Fetch.Entity.NoAttributes = Me.chkNoAttrib.Checked
            Me.Fetch.Aggregate = Me.chkAggregate.Checked
            Me.Fetch.Count = Integer.Parse(Trim(Me.txtCount.Text))
            'If Not Me.chkAllAttributes.Checked And Not Me.chkNoAttrib.Checked Then
            '    If Me.Fetch.Entity.Attributes.Count = 0 Then
            '        Windows.Forms.MessageBox.Show("Select at least one attribute or check all attributes.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            '        Me.Cursor = Cursors.Default
            '        Exit Sub
            '    Else
            '        Me.Fetch.Entity.Attributes.Clear()
            '        Dim att As String
            '        For Each att In Me.lstAttributes.CheckedItems
            '            Me.Fetch.Entity.Attributes.Add(New Attribute(att))
            '        Next
            '    End If
            'End If
            Dim doc As New Xml.XmlDocument
            Dim XMLQuery As String = Me.Fetch.ToXML
            doc.LoadXml(XMLQuery)
            Me.txtQuery.Text = FormatXML(doc)
            Me.tabsMain.SelectTab(Me.tabQuery)
            Me.Cursor = Cursors.Default
        Catch exc As Exception
            'todo: change error message
            Me.Cursor = Cursors.Default
            Windows.Forms.MessageBox.Show(exc.Message)
        End Try
    End Sub

    Private Sub btnResults_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResults.Click
        Try
            If Not Me.txtQuery.Text.Length > 0 Then
                btnCreateXML_Click(Nothing, Nothing)
                If Not Me.txtQuery.Text.Length > 0 Then
                    Exit Sub
                End If
            End If
            Me.Cursor = Cursors.WaitCursor
            Dim service As New CrmService
            service.Credentials = Helpers.cred
            service.Url = Me.CrmServiceURL
            Dim doc As New Xml.XmlDocument
            Dim result As String
            result = service.Fetch(Me.txtQuery.Text)
            doc.LoadXml(result)
            Me.txtResult.Text = FormatXML(doc.DocumentElement)
            Me.lblResults.Text = doc.DocumentElement.ChildNodes.Count & " " & Me.Fetch.Entity.Name & " entities returned."
            Me.tabsMain.SelectTab(Me.tabResults)
            Me.Cursor = Cursors.Default
        Catch soapexc As System.Web.Services.Protocols.SoapException
            Me.Cursor = Cursors.Default
            Me.txtResult.Text = soapexc.Detail.InnerXml & vbCrLf
            Me.lblResults.Text = ""
            If Not soapexc.InnerException Is Nothing Then
                Me.txtResult.Text &= soapexc.InnerException.Message
            End If
            Me.tabsMain.SelectTab(Me.tabResults)
        Catch exc As Exception
            Me.txtResult.Text = exc.Message
            Me.lblResults.Text = ""
            Me.Cursor = Cursors.Default
            Me.tabsMain.SelectTab(Me.tabResults)
        End Try
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub SaveFetchXMLToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFetchXMLToolStripMenuItem.Click
        If Not Me.txtQuery.Text.Length > 0 Then
            btnCreateXML_Click(Nothing, Nothing)
            If Not Me.txtQuery.Text.Length > 0 Then
                Exit Sub
            End If
        End If
        Dim filename As String
        Dim dialog As New Windows.Forms.SaveFileDialog()
        dialog.AddExtension = True
        dialog.DefaultExt = "xml"
        If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            filename = dialog.FileName
        Else
            Exit Sub
        End If
        Dim f As New IO.FileStream(filename, IO.FileMode.Create, IO.FileAccess.Write)
        Dim writer As New IO.StreamWriter(f)
        writer.WriteLine(Me.txtQuery.Text)
        writer.Flush()
        f.Close()
        Me.lblResults.Text = ""
    End Sub

    Private Sub SaveResultsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultsToolStripMenuItem.Click
        If Not Me.txtResult.Text.Length > 0 Then
            If Me.txtQuery.Text.Length > 0 Then
                btnResults_Click(Nothing, Nothing)
            Else
                btnCreateXML_Click(Nothing, Nothing)
                If Me.txtQuery.Text.Length > 0 Then
                    btnResults_Click(Nothing, Nothing)
                Else
                    Exit Sub
                End If
            End If
        End If
        Dim filename As String
        Dim dialog As New Windows.Forms.SaveFileDialog()
        dialog.AddExtension = True
        dialog.DefaultExt = "xml"
        If dialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            filename = dialog.FileName
        Else
            Exit Sub
        End If
        Dim f As New IO.FileStream(filename, IO.FileMode.Create, IO.FileAccess.Write)
        Dim writer As New IO.StreamWriter(f)
        writer.WriteLine(Me.txtResult.Text)
        writer.Flush()
        f.Close()
        Me.lblResults.Text = ""
    End Sub

    Private Sub chkAllAttributes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAllAttributes.CheckedChanged
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
        Me.lblResults.Text = ""
        If Me.chkNoAttrib.Checked Then
            Me.chkNoAttrib.Checked = False
        End If
    End Sub

    Private Sub chNoAttributes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkNoAttrib.CheckedChanged
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
        Me.lblResults.Text = ""
        If Me.chkAllAttributes.Checked Then
            Me.chkAllAttributes.Checked = False
        End If
    End Sub

    Private Sub lstAttributes_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs)
        Me.txtResult.Text = ""
        Me.txtQuery.Text = ""
        Me.lblResults.Text = ""
    End Sub

    Private Sub txtQuery_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQuery.TextChanged
        Me.txtResult.Text = ""
        Me.lblResults.Text = ""
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        Dim frm As New frmAbout()
        frm.ShowDialog()
    End Sub

    Private Sub UserQueryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserQueryToolStripMenuItem.Click
        Dim frm As New frmQuery(Me.CrmServiceURL, QueryType.User)
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim doc As New Xml.XmlDocument
            doc.LoadXml(frm.QueryFetchXML)
            Me.tabsMain.SelectTab(Me.tabQuery)
            Me.Fetch = New Fetch(doc.DocumentElement)
            Me.txtResult.Text = ""
            Me.lblResults.Text = ""
            SuppressEntityChangeAlert = True
            Me.RefreshControls()
            SuppressEntityChangeAlert = False
            Me.txtQuery.Text = FormatXML(doc)
        End If
    End Sub

    Private Sub SavedQueryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavedQueryToolStripMenuItem.Click
        Dim frm As New frmQuery(Me.CrmServiceURL, QueryType.Saved)
        Me.Cursor = Cursors.WaitCursor
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim doc As New Xml.XmlDocument
            doc.LoadXml(frm.QueryFetchXML)
            Me.tabsMain.SelectTab(Me.tabQuery)
            Me.Fetch = New Fetch(doc.DocumentElement)
            Me.txtResult.Text = ""
            Me.lblResults.Text = ""
            SuppressEntityChangeAlert = True
            Me.RefreshControls()
            SuppressEntityChangeAlert = False
            Me.txtQuery.Text = FormatXML(doc)
        End If
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub RefreshControls()
        Me.lstEntity.SelectedIndex = (Me.lstEntity.FindStringExact(Me.Fetch.Entity.Name))
        LoadAttributes()
        LoadFilters()
        LoadLinkedEntities()
        LoadOrders()
        If Me.Fetch.Entity.AllAttributes Then
            Me.chkAllAttributes.Checked = True
        Else
            Me.chkAllAttributes.Checked = False
        End If
        If Me.Fetch.Entity.NoAttributes Then
            Me.chkNoAttrib.Checked = True
        Else
            Me.chkNoAttrib.Checked = False
        End If
        Me.chkAggregate.Checked = Me.Fetch.Aggregate
        If Me.Fetch.Count > 0 Then
            Me.txtCount.Text = Me.Fetch.Count
        End If
        'For Each attribute In Me.Fetch.Entity.Attributes
        '    Me.lstAttributes.SetItemCheckState(Me.lstAttributes.FindString(attribute.ToString), CheckState.Checked)
        'Next
    End Sub

    Private Sub btnAttributes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAttributes.Click
        Dim frmAttrib As New frmAttribute(Me.Fetch.Entity)
        If frmAttrib.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadAttributes()
            Me.txtQuery.Text = ""
            Me.txtResult.Text = ""
            Me.lblResults.Text = ""
        End If
    End Sub

    Private Sub FileToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem1.Click
        Dim myStream As System.IO.Stream
        Dim openFileDialog1 As New OpenFileDialog()

        openFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        openFileDialog1.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 2
        openFileDialog1.RestoreDirectory = True

        If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            myStream = openFileDialog1.OpenFile()
            If Not (myStream Is Nothing) Then
                Try
                    Dim streamreader As New IO.StreamReader(myStream)
                    Dim xml As String = streamreader.ReadToEnd
                    Dim doc As New Xml.XmlDocument
                    doc.LoadXml(xml)
                    Me.tabsMain.SelectTab(Me.tabQuery)
                    Me.Fetch = New Fetch(doc.DocumentElement)
                    Me.txtResult.Text = ""
                    Me.lblResults.Text = ""
                    SuppressEntityChangeAlert = True
                    Me.RefreshControls()
                    SuppressEntityChangeAlert = False
                    Me.txtQuery.Text = FormatXML(doc)
                    myStream.Close()
                Catch exc As Exception
                    Windows.Forms.MessageBox.Show("Error importing file. Check that file is in proper FetchXML syntax.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try
            End If
        End If

    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        Try
            Dim doc As New Xml.XmlDocument
            doc.LoadXml(My.Computer.Clipboard.GetText())
            Me.tabsMain.SelectTab(Me.tabQuery)
            Me.Fetch = New Fetch(doc.DocumentElement)
            Me.txtResult.Text = ""
            Me.lblResults.Text = ""
            SuppressEntityChangeAlert = True
            Me.RefreshControls()
            SuppressEntityChangeAlert = False
            Me.txtQuery.Text = FormatXML(doc)
        Catch exc As Exception
            Windows.Forms.MessageBox.Show("Error pasting. Check that the copied text is in proper FetchXML syntax.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub txtCount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCount.TextChanged
        If Trim(Me.txtCount.Text).Length > 0 Then
            Dim result As Integer
            If Not Integer.TryParse(Trim(Me.txtCount.Text), result) Then
                Me.txtCount.Text = ""
                Windows.Forms.MessageBox.Show("Count must be an integer.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End If
    End Sub

    Private Sub lstFilters_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFilters.SelectedIndexChanged

    End Sub
End Class


